<?php

class m130626_164047_init_db extends CDbMigration
{
	public function up()
	{
		$sql = "
			SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
			SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
			SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

			CREATE SCHEMA IF NOT EXISTS `gameonline` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
			USE `gameonline` ;

			-- -----------------------------------------------------
			-- Table `gameonline`.`users`
			-- -----------------------------------------------------
			CREATE  TABLE IF NOT EXISTS `gameonline`.`users` (
			  `id` INT NOT NULL AUTO_INCREMENT ,
			  `username` VARCHAR(255) NOT NULL ,
			  `email` VARCHAR(255) NOT NULL ,
			  `password` VARCHAR(128) NOT NULL ,
			  `salt` VARCHAR(13) NOT NULL ,
			  `timezone` VARCHAR(100) NULL ,
			  `created_at` TIMESTAMP NULL ,
			  `updated_at` TIMESTAMP NULL ,
			  `amount` DOUBLE NOT NULL ,
			  `currency` VARCHAR(45) NOT NULL ,
			  PRIMARY KEY (`id`) ,
			  UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
			  UNIQUE INDEX `email_UNIQUE` (`email` ASC) )
			ENGINE = InnoDB
			DEFAULT CHARACTER SET = utf8
			COLLATE = utf8_unicode_ci;


			-- -----------------------------------------------------
			-- Table `gameonline`.`user_profiles`
			-- -----------------------------------------------------
			CREATE  TABLE IF NOT EXISTS `gameonline`.`user_profiles` (
			  `id` INT NOT NULL AUTO_INCREMENT ,
			  `first_name` VARCHAR(100) NULL ,
			  `last_name` VARCHAR(100) NULL ,
			  `full_name` VARCHAR(255) NULL ,
			  `user_id` INT NOT NULL ,
			  PRIMARY KEY (`id`) ,
			  INDEX `fk_user_profiles_users_idx` (`user_id` ASC) ,
			  CONSTRAINT `fk_user_profiles_users`
			    FOREIGN KEY (`user_id` )
			    REFERENCES `gameonline`.`users` (`id` )
			    ON DELETE NO ACTION
			    ON UPDATE NO ACTION)
			ENGINE = InnoDB
			DEFAULT CHARACTER SET = utf8
			COLLATE = utf8_unicode_ci;


			-- -----------------------------------------------------
			-- Table `gameonline`.`transactions`
			-- -----------------------------------------------------
			CREATE  TABLE IF NOT EXISTS `gameonline`.`transactions` (
			  `id` INT NOT NULL AUTO_INCREMENT ,
			  `type` VARCHAR(50) NOT NULL ,
			  `user_id` INT NOT NULL ,
			  `cash` DOUBLE NOT NULL ,
			  `vcash` DOUBLE NOT NULL ,
			  `rate` FLOAT NULL ,
			  `from` VARCHAR(50) NULL ,
			  `from_detail` TEXT NULL ,
			  `tran_cert` VARCHAR(128) NOT NULL ,
			  `created_at` TIMESTAMP NOT NULL ,
			  PRIMARY KEY (`id`) ,
			  INDEX `fk_transactions_users1_idx` (`user_id` ASC) ,
			  CONSTRAINT `fk_transactions_users1`
			    FOREIGN KEY (`user_id` )
			    REFERENCES `gameonline`.`users` (`id` )
			    ON DELETE NO ACTION
			    ON UPDATE NO ACTION)
			ENGINE = InnoDB
			DEFAULT CHARACTER SET = utf8
			COLLATE = utf8_unicode_ci;

			USE `gameonline` ;


			SET SQL_MODE=@OLD_SQL_MODE;
			SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
			SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
		";
		$this->execute($sql);
		return true;
	}

	public function down()
	{
		echo "m130626_164047_init_db does not support migration down.\n";
		return false;
	}

	/*
	// Use safeUp/safeDown to do migration with transaction
	public function safeUp()
	{
	}

	public function safeDown()
	{
	}
	*/
}